//
//  SPResetPasswordViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/3/31.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPResetPasswordViewController.h"
#import "UIColor+CreateMethod.h"
#import "SPHTTPRequestOperationManager.h"
#import "Methods.h"

@interface SPResetPasswordViewController (){
    UITextField * m_passwordField;
    UITextField * m_confirmPasswordField;
    float leftoffset;
}

@end

@implementation SPResetPasswordViewController
@synthesize infoDic;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    leftoffset = 50;
    
    m_passwordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, 50, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_passwordField setBackgroundColor:[UIColor clearColor]];
    [m_passwordField setPlaceholder:@"密码"];
    [self.view addSubview:m_passwordField];
    
    UIImageView * usernameIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_passwordField.frame.origin.y, 20, 20)];
    [usernameIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:usernameIcon];
    
    m_confirmPasswordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, m_passwordField.frame.origin.y + m_passwordField.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_confirmPasswordField setBackgroundColor:[UIColor clearColor]];
    [m_confirmPasswordField setPlaceholder:@"确认密码"];
    [self.view addSubview:m_confirmPasswordField];
    
    UIImageView * paswordIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_confirmPasswordField.frame.origin.y, 20, 20)];
    [paswordIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:paswordIcon];
    
    UIButton * nextButton = [[UIButton alloc]initWithFrame:CGRectMake(0, m_confirmPasswordField.frame.origin.y + m_confirmPasswordField.frame.size.height + 10, self.view.frame.size.width, 40)];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"login_login_btn"] forState:UIControlStateNormal];
    [nextButton setTitle:@"确认" forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(nextButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, nextButton.frame.origin.y + nextButton.frame.size.height + 30, self.view.frame.size.width, 40)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor colorWith8BitRed:122 green:177 blue:226 alpha:1] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignTextField)];
    tapBg.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapBg];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backButtonListener{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)resignTextField{
    [m_passwordField resignFirstResponder];
    [m_confirmPasswordField resignFirstResponder];
}

- (void)nextButtonListener{
    if (m_passwordField.text.length == 0) {
        [self showHintProgressWithText:@"请输入新密码"];
    }
    else if (m_confirmPasswordField.text.length == 0){
        [self showHintProgressWithText:@"请输入确认密码"];
    }
    else if (![m_passwordField.text isEqualToString:m_confirmPasswordField.text]){
        [self showHintProgressWithText:@"两次输入密码不一致"];
    }
    else{
        if ([Methods isExistenceNetwork]) {
            [self resetPassword];
        }
        else{
            [self showHintProgressWithText:@"无可用网络"];
        }
    }
}

- (void)resetPassword{
    [self showProgressAutoCloseInNetwork:@"重设密码中"];
    [self.infoDic setValue:[Methods md5:m_passwordField.text] forKey:@"newpwd"];
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    [manager resetPasswordPara:self.infoDic Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"reset_password>>>>>>>>SUCCESS: %@\nmessage:%@",responseObject,[responseObject valueForKey:@"message"]);
        
        if ([[responseObject valueForKey:@"is_success"] intValue] != 0) {
            [self showHintProgressWithText:@"密码修改成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else{
            [self closeProgress];
            [self showHintProgressWithText:[responseObject valueForKey:@"message"]];
        }
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"reset_password>>>>>>>>Error: %@", error);
        [self closeProgress];
        [self showHintProgressWithText:@"密码设置失败"];
    }];
}

@end
